import pyqtgraph as pg
from PyQt6 import QtWidgets, QtCore


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.plot_graph = pg.PlotWidget()
        self.setCentralWidget(self.plot_graph)
        self.plot_graph.setBackground("w")  # 背景色设置为白色
        # self.plot_graph.setBackground("#bbccaa")  # 使用十六进制
        # self.plot_graph.setBackground((100, 50, 255))  # RGB each 0-255
        # self.plot_graph.setBackground((100, 50, 255, 25))  # RGBA (A = alpha opacity)
        # self.plot_graph.setBackground(QtGui.QColor(100, 50, 254, 25))

        # 使用单个字母有许多颜色可用。它们基于Matplotlib中使用的标准颜色。
        # 常用的颜色代码：
        # "b" Blue 蓝色的
        # "c" Cian 清洁
        # "d" Grey 灰色
        # "g" Green 绿色
        # "k" Black 黑色的
        # "m" Magenta 品红色的
        # "r" Red 红色的
        # "w" White 白色
        # "y" Yellow 黄色的

        minutes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        temperature_1 = [30, 32, 34, 32, 33, 31, 29, 32, 35, 30]
        temperature_2 = [32, 35, 40, 22, 38, 32, 27, 38, 32, 38]

        self.plot_graph.setTitle("Temperature vs Time", color="b", size="20pt")

        # 使用HTML标签和CSS样式
        # self.plot_graph.setTitle(
        #     '<span style="color: blue; font-size: 20pt">Temperature vs Time</span>'
        # )

        styles = {"color": "red", "font-size": "18px"}
        self.plot_graph.setLabel("left", "Temperature (°C)", **styles)
        self.plot_graph.setLabel("bottom", "Time (min)", **styles)

        # 添加图例
        self.plot_graph.addLegend()

        # 显示背景网格
        self.plot_graph.showGrid(x=True, y=True)

        # 设置坐标轴的范围
        self.plot_graph.setXRange(1, 10)
        self.plot_graph.setYRange(20, 40)   

        # 指定线条样式
        # pen = pg.mkPen(color=(255, 0, 0), width=5, style=QtCore.Qt.PenStyle.DashLine)
        pen = pg.mkPen(color=(255, 0, 0))
        self.plot_line("Temperature Sensor 1", minutes, temperature_1, pen, "b")
        pen = pg.mkPen(color=(0, 0, 255))
        self.plot_line("Temperature Sensor 2", minutes, temperature_2, pen, "r")

    def plot_line(self, name, minutes, temperature, pen, brush):
        self.plot_graph.plot(
            minutes,
            temperature,
            name=name,
            pen=pen,
            symbol="+",
            symbolSize=15,
            symbolBrush=brush,
        )

app = QtWidgets.QApplication([])
main = MainWindow()
main.show()
app.exec()
